import path from "path"
// import fs from "fs-extra"
import puppeteer from "puppeteer-core"
// import env from "../env.duty"
import { killChrome, setNetInterface } from "@/utils"
// import { IIpMap, ITask } from "../src/interface"
import { IFolderName } from "@/interface"
import dutyConfigJson from "env.duty.json"

const main = async (folderName: IFolderName) => {
  await killChrome().catch(e => e)

  // const env = <ITask>fs.readJSONSync(path.join(__dirname, "../env.duty.json"))

  const { ip } = <{ ip: string }>(
    dutyConfigJson.ipMap.find(i => i.folder == folderName)
  )

  // const { ip, cacheName } = <IIpMap>env.ipMap.find(i => i.floder == folderName)

  await setNetInterface(ip)

  const userDataDir = path.join(".cache", ip)

  const browser = await puppeteer.launch({
    headless: false,
    channel: "chrome",
    defaultViewport: null,
    userDataDir,
    timeout: 0,

    args: [
      "--no-sandbox",
      "--disable-setuid-sandbox",
      "--disable-notifications",
    ],
  })

  const [page] = await browser.pages()

  await page.waitForTimeout(2000)

  await page.goto("https://www.bilibili.com/", {
    waitUntil: "load",
  })
}

main

// execSync(
//   'start "" "C:Program FilesGoogleChromeApplicationchrome.exe" --user-data-dir="C:path\toprofile"',
// )
